<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <my-bread :level1="'角色管理'" :level2="'角色列表'" />
      <el-button style="margin-top:20px" type="info">添加</el-button>
    </div>
    <el-table :data="roleList" style="width: 100%" height="660">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-row v-for="(item1,i) in scope.row.children" :key="i">
            <el-col :span="4">
              <el-tag
                class="tag"
                type="success"
                closable
                @close="delRight(scope.row,item1.id)"
              >{{item1.authName}}</el-tag>
              <i class="el-icon-d-arrow-right"></i>
            </el-col>
            <el-col :span="20">
              <el-row v-for="(item2,i) in item1.children" :key="i">
                <el-col :span="4">
                  <el-tag
                    class="tag"
                    type="info"
                    closable
                    @close="delRight(scope.row,item2.id)"
                  >{{item2.authName}}</el-tag>
                  <i class="el-icon-d-arrow-right"></i>
                </el-col>
                <el-col :span="20">
                  <el-tag
                    class="tag"
                    closable
                    v-for="(item3,i) in item2.children"
                    :key="i"
                    @close="delRight(scope.row,item3.id)"
                  >{{item3.authName}}</el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <el-alert
            v-if="scope.row.children.length == 0"
            title="未分配权限"
            type="warning"
            :closable="false"
          ></el-alert>
        </template>
      </el-table-column>
      <el-table-column type="index" align="center"></el-table-column>
      <el-table-column label="角色名称" prop="roleName"></el-table-column>
      <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
      <el-table-column prop="address" label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle @click></el-button>
          <el-button
            type="success"
            icon="el-icon-check"
            circle
            @click="showSetRightRole(scope.row)"
          ></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改权限" :visible.sync="dialogVisibleEdit" width="40%">
      <el-tree
        :data="treeList"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="treeCheck"
        :props="{children: 'children',label: 'authName'}"
        ref="tree"
      ></el-tree>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="setRoleRight">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>
<script>
export default {
  data() {
    return {
      roleList: [],
      treeList: [],
      arrexpand: [],
      treeCheck: [],
      currRoleId: -1,
      dialogVisibleEdit: false
    };
  },
  created() {
    this.getRoleList();
  },
  methods: {
    async setRoleRight() {
      let arr1 = this.$refs.tree.getCheckedKeys();
      let arr2 = this.$refs.tree.getHalfCheckedKeys();
      let arr = [...arr1, ...arr2];
      const res = await this.$axios.post(`roles/${this.currRoleId}/rights`, {
        rids: arr.join(",")
      });
      if (res.data.meta.status === 200) {
        this.$message.success(res.data.meta.msg);
        this.dialogVisibleEdit = false;
        this.getRoleList();
      }
    },
    async showSetRightRole(role) {
      this.dialogVisibleEdit = true;
      this.currRoleId = role.id;
      const res = await this.$axios.get(`rights/tree`);
      this.treeList = res.data.data;
      let arrCheck = [];
      role.children.forEach(item1 => {
        item1.children.forEach(item2 => {
          item2.children.forEach(item3 => {
            arrCheck.push(item3.id);
          });
        });
      });
      this.treeCheck = arrCheck;
    },
    async delRight(role, rightId) {
      const res = await this.$axios.delete(
        `roles/${role.id}/rights/${rightId}`
      );
      if (res.data.meta.status === 200) {
        this.$message.success(res.data.meta.msg);
        role.children = res.data.data;
      }
    },
    async getRoleList() {
      const res = await this.$axios.get("roles");
      this.roleList = res.data.data;
    }
  }
};
</script>

<style lang="scss">
.tag {
  margin: 2px;
}
</style>

